package jl.com.controller;

import com.alibaba.fastjson.JSON;
import jl.com.aliyun.config.ConfigProperties;
import jl.com.entity.WxNumber;
import jl.com.response.JsonResult;
import jl.com.response.ResultCode;
import jl.com.service.ArticeSpiderService;
import jl.com.util.DateUtil;
import jl.com.util.JdbcDao;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

public class ArticeSpiderController {

    private final static Logger log = LoggerFactory.getLogger(ArticeSpiderController.class);
    static JdbcDao jdbcDao = new JdbcDao();
    /**
     * 根据数据库内微信号文章数据抓取
     * @throws Exception
     */
    public static void   spiderDatabase()throws  Exception{
//        System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");//本地路径
        System.setProperty("webdriver.chrome.driver", "spider/chromedriver");//linux路径
        //启动一个无头模式的chrome 实例
        Date startDate = new Date();
        log.info("批量抓取文章开始,时间："+DateUtil.dateToStringDate(new Date())+"===================================================");
        int number = 0;
        WebDriver driver = null;
        try {
            List<WxNumber> wxNumbers = jdbcDao.selectAllCategoryMpService();
            if (wxNumbers != null) log.info("================公众号数量:"+wxNumbers.size());
            for (int i = 0; i<wxNumbers.size(); i++){
                try {
                    number++;
                    driver = ArticeSpiderService.headDriver();
                    log.info("================第" + number + "次");
                    //打开一个站点
                    driver.get(ConfigProperties.resource.getString("sougou.url"));
                    //进入文章详情页获取文章页url
                    driver =  ArticeSpiderService.getArticleUrl(driver, wxNumbers.get(i));
                    //文章页url
                    String url = driver.getCurrentUrl();
                    log.info("================url:"+url);
                    //根据url抓取文章
                    ArticeSpiderService.spiderUrl(url, wxNumbers.get(i));
                    log.info("================"+wxNumbers.get(i).getId()+"的文章提取完成");
                    Thread.sleep(DateUtil.returnDoubleTime(5));
                }catch (Exception e){
                    number--;
                    log.error("================"+JSON.toJSON(new JsonResult(ResultCode.EXCEPTION,"失败",e.getMessage())));
                    driver.quit();
                    continue;
                }finally {
                    driver.quit();
                }
            }
        }catch (Exception e) {
            log.error("================"+JSON.toJSON(new JsonResult(ResultCode.EXCEPTION,"失败",e.getMessage())));
            driver.quit();
        } finally {
            log.info("================批量抓取文章完成,共"+number+"篇,共耗时" + (new Date().getTime() - startDate.getTime()) / 1000 + "s,时间："+DateUtil.dateToStringDate(new Date()));
            driver.quit();
        }
    }


}
